//
//  NSString+category.m
//  MoCloud
//
//  Created by xj on 2019/5/28.
//  Copyright © 2019 xj. All rights reserved.
//

#import "NSString+category.h"


@implementation NSString (category)

- (NSString *)localizedString {
    NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:appLanguage];
    NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",language] ofType:@"lproj"];
    return [[NSBundle bundleWithPath:path] localizedStringForKey:(self) value:nil table:@"Localizable"];
}

- (instancetype)tj_cachePath {
    return  [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[self lastPathComponent]];
}

- (instancetype)tj_documentsPath {
    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[self lastPathComponent]];
}

- (instancetype)tj_tempPath {
    return [NSTemporaryDirectory() stringByAppendingPathComponent:[self lastPathComponent]];
}
/**消空格 **/
- (nonnull NSString *)replacingEmptyString {
    if (!self) return nil;
    if (![self isKindOfClass:NSString.class]) return @"";
    if ([self containsString:@" "]) return [self stringByReplacingOccurrencesOfString:@" " withString:@""];
    return self;
}

- (CGSize)getStringSizeWithWidth:(CGFloat)width fontSize:(CGFloat)fontSize {
    return  [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size;
}

- (CGSize)getStringSizeWithWidth:(CGFloat)width font:(UIFont *)font {
    return  [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:nil].size;
}


- (NSAttributedString *)titleKernSpacing:(CGFloat)kernSpacing {
    NSMutableAttributedString *attributedString =  [[NSMutableAttributedString alloc] initWithString:self attributes:@{NSKernAttributeName : @(kernSpacing)}];
    return attributedString;
}

- (NSAttributedString *)titleMargin:(CGFloat)magrin withAlignment:(NSTextAlignment)alignment {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:magrin];
    paragraphStyle.alignment = alignment;
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, self.length)];
    return attributedString;
}


+ (NSString *)dateStringWithDateFormat:(NSDateFormatter *)dateFormat timeInterval:(NSTimeInterval)timeInterval {
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    
    if (!dateFormat) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        return  [formatter stringFromDate:date];
    }
    return  [dateFormat stringFromDate:date];
    
 
}

+ (float)floatWithdecimalNumber:(double)num {
    return [[self decimalNumber:num] floatValue];
}

+ (double)doubleWithdecimalNumber:(double)num {
    return [[self decimalNumber:num] doubleValue];
}

+ (NSString *)stringWithDecimalNumber:(double)num {
    return [[self decimalNumber:num] stringValue];
}

+ (NSDecimalNumber *)decimalNumber:(double)num {
    NSString *numString = [NSString stringWithFormat:@"%lf", num];
    return [NSDecimalNumber decimalNumberWithString:numString];
}

- (nonnull NSAttributedString *)attributedStringWithLoction:(NSInteger)loction foregroundFont:(CGFloat)foregroundFont backgroundFont:(CGFloat)backgroundFont color:(nonnull UIColor *)color {
    NSMutableAttributedString *newAttrStr = [[NSMutableAttributedString alloc] initWithString: self];
    [newAttrStr addAttribute:NSFontAttributeName value:UIFont_Px(foregroundFont) range:NSMakeRange(0,loction)];
    [newAttrStr addAttribute:NSFontAttributeName value:UIFont_Px(backgroundFont) range:NSMakeRange(loction,self.length - loction)];
    [newAttrStr addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0,self.length)];
    return  newAttrStr.copy;
}

- (nonnull NSAttributedString *)attributedStringWithLoction:(NSInteger)loction foregroundFont:(CGFloat)foregroundFont backgroundFont:(CGFloat)backgroundFont foregroundcolor:(nonnull UIColor *)foregroundcolor backgroundColor:(nonnull UIColor *)backgroundColor {
    NSMutableAttributedString *newAttrStr = [[NSMutableAttributedString alloc] initWithString: self];
    [newAttrStr addAttribute:NSFontAttributeName value:UIFont_Px(foregroundFont) range:NSMakeRange(0,(loction))];
    [newAttrStr addAttribute:NSFontAttributeName value:UIFont_Px(backgroundFont) range:NSMakeRange(loction,self.length - (loction))];
    [newAttrStr addAttribute:NSForegroundColorAttributeName value:foregroundcolor range:NSMakeRange(0,self.length)];
    [newAttrStr addAttribute:NSForegroundColorAttributeName value:backgroundColor range:NSMakeRange(loction,self.length - (loction))];
    return newAttrStr.copy;
    
}

- (nonnull NSAttributedString *)attributedStringWithLoction:(NSInteger)loction foregroundFont:(CGFloat)foregroundFont backgroundFont:(CGFloat)backgroundFont weight:(CGFloat)weight color:(nonnull UIColor *)color {
    NSMutableAttributedString *newAttrStr = [[NSMutableAttributedString alloc] initWithString: self];
    if (@available(iOS 8.2, *)) {
        [newAttrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:foregroundFont weight:weight] range:NSMakeRange(0,loction)];
        [newAttrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:backgroundFont weight:weight] range:NSMakeRange(loction,self.length - loction)];
    } else {
        [newAttrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:foregroundFont] range:NSMakeRange(0,loction)];
        [newAttrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:backgroundFont] range:NSMakeRange(loction,self.length - loction)];
    }
    [newAttrStr addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0,self.length)];
    return  newAttrStr.copy;
}

- (nonnull NSAttributedString *)attributedStringWithRanger:(NSRange)ranger foregroundFont:(CGFloat)foregroundFont backgroundFont:(CGFloat)backgroundFont foregroundcolor:(nonnull UIColor *)foregroundcolor backgroundColor:(nonnull UIColor *)backgroundColor {
    
    NSMutableAttributedString *newAttrStr = [[NSMutableAttributedString alloc] initWithString: self];
    [newAttrStr addAttribute:NSFontAttributeName value:UIFont_Px(foregroundFont) range:ranger];
    [newAttrStr addAttribute:NSFontAttributeName value:UIFont_Px(backgroundFont) range:ranger];
    [newAttrStr addAttribute:NSFontAttributeName value:UIFont_Px(foregroundFont) range:ranger];
    
    [newAttrStr addAttribute:NSForegroundColorAttributeName value:foregroundcolor range:ranger];
    [newAttrStr addAttribute:NSForegroundColorAttributeName value:backgroundColor range:ranger];
    [newAttrStr addAttribute:NSForegroundColorAttributeName value:foregroundcolor range:ranger];
    
    return  newAttrStr.copy;
}

- (nonnull NSAttributedString *)attributedStringWithNumberString:(nonnull NSString *)numberString numberStringColor:(nullable UIColor *)numberStringColor {
    NSMutableAttributedString *newAttrStr = [[NSMutableAttributedString alloc] initWithString:self];
    [newAttrStr addAttribute:NSForegroundColorAttributeName value:numberStringColor range:[self rangeOfString:numberString]];
    return newAttrStr;
}

- (nonnull NSAttributedString *)titleMargin:(CGFloat)magrin highlightedString:(NSString *)highlightedString highlightedColor:(UIColor *)highlightedColor {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:magrin];
    paragraphStyle.alignment = NSTextAlignmentLeft;
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, self.length)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:highlightedColor range:[self rangeOfString:highlightedString]];
    return attributedString;
}

#pragma mark - 获取联系人姓名首字母(传入汉字字符串, 返回大写拼音首字母)
- (NSString *)getFirstLetterString {
    /**
     * **************************************** START ***************************************
     * 之前PPGetAddressBook对联系人排序时在中文转拼音这一部分非常耗时
     * 参考博主-庞海礁先生的一文:iOS开发中如何更快的实现汉字转拼音 http://www.olinone.com/?p=131
     * 使PPGetAddressBook对联系人排序的性能提升 3~6倍, 非常感谢!
     */
    NSMutableString *mutableString = [NSMutableString stringWithString:self];
    CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
    NSString *pinyinString = [mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
    /**
     *  *************************************** END ******************************************
     */
    
    // 将拼音首字母装换成大写
    NSString *strPinYin = [[self polyphoneStringHandle:self pinyinString:pinyinString] uppercaseString];
    // 截取大写首字母
    NSString *firstString = @"#";
    if (strPinYin.length > 1) {
       firstString = [strPinYin substringToIndex:1];
    }
    // 判断姓名首位是否为大写字母
    NSString * regexA = @"^[A-Z]$";
    NSPredicate *predA = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexA];
    // 获取并返回首字母
    return [predA evaluateWithObject:firstString] ? firstString : @"#";
    
}

- (NSString *)polyphoneStringHandle:(NSString *)aString pinyinString:(NSString *)pinyinString {
    if ([aString hasPrefix:@"长"]) { return @"chang";}
    if ([aString hasPrefix:@"沈"]) { return @"shen"; }
    if ([aString hasPrefix:@"厦"]) { return @"xia";  }
    if ([aString hasPrefix:@"地"]) { return @"di";   }
    if ([aString hasPrefix:@"重"]) { return @"chong";}
    return pinyinString;
}


@end
